<?php
/**
 * Created by PhpStorm.
 * Author: Fedax
 * Date: 2017/6/30
 * Time: 17:41
 */

namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use app\admin\model\Role as RoleModel;
use app\admin\model\User;

/**
 * 管理员
 * Class Admin
 * @package app\admin\controller
 */
class Admin extends Index
{
    /**
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $roleModel = new RoleModel();
        $page_config = array(
            'page' => input('page', 1),
            'list_rows' => input('list_rows', 10),
        );
        $data = $roleModel->getDataList($page_config);
        $this->assign('roleList', $data);
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function indexData(){
        $adminModel = new AdminModel();
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('list_rows', 10),
        );
        $data = $adminModel->getDataList($page_config);
        return json($data);
    }

    /**
     * 添加
     * @return \think\response\View
     */
    public function save()
    {
        if(request()->isPost()){
//            $data=array();
//            $data['admin_name']=input('admin_name');
//            $data['admin_tel']=input('admin_tel');
//            $data['admin_password']=input('admin_password');
//            $data['role_id']=input('role_id');
//            $validate = validate('Admin');
//            if(!$validate->scene('add')->check($data)){
//                $this->error($validate->getError());
//            };
            $adminModel = new AdminModel();
            $newData = array();
            $newData['admin_name'] = input('admin_name');
            $newData['admin_password'] = md5(trim(input('admin_password')));
            $newData['role_id'] = input('role_id');
            $re = $adminModel->save($newData);
            if ($re) {
            $this->success(lang('add') . lang('success'));
            }
            $this->error(lang('add') . lang('false'));
            }

        return redirect('/Miss');
    }

    /**
     * 删除
     * @return \think\response\Redirect
     */
    public function delete()
    {
        if(request()->isPost()){
            $adminModel = new AdminModel();
            if ($adminModel->destroy(input('id'))) {
                $this->success(lang('delete') . lang('success'));
            }
            $this->error(lang('delete') . lang('false'));
        }
        return redirect('/Miss');
    }

    /**
     * 编辑
     * @return \think\response\Redirect
     * @throws \think\exception\DbException
     */
    public function edit(){
        if (request()->isPost()) {
            $adminModel = new AdminModel();
            $data = $adminModel->getOrFail(input('id'));
            $data->admin_name = input('admin_name');
            $data->role_id = input('role_id');
            $data->admin_password = md5(trim(input('admin_password')));
            if ($data->save()) {
                $this->success(lang('edit') . lang('success'));
            }
            $this->error(lang('edit') . lang('false'));
        }
        return redirect('/Miss');
    }
}